package cn.yl.providerHystrix.controller;



import cn.yl.providerHystrix.config.MyAnnotation;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.UUID;
import java.util.concurrent.TimeUnit;

@RestController
@RequestMapping("/provider")
public class ProviderController {


    @GetMapping("hystrix/ok")
    public String hystrixOk() {
        int a=10/0;
        return Thread.currentThread().getName()+" --providerHystrix --  ok  !!";
    }
//fallbackMethod = "providerHystrixTimeOutHandler",
    @HystrixCommand(commandProperties = {
            @HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds", value = "2000")
    })
    @GetMapping("/hystrix/timeout")
    public String providerFeignTimeOut() {
        int time=3;
//        int a=10/0;
        try {
            // 休眠3秒
            TimeUnit.SECONDS.sleep(time);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return  Thread.currentThread().getName()+"  --  out --  providerHystrixTimeOut  耗时  ==  " +time;
    }

    @GetMapping("/hystrix/order/{id}")
    public String providerOrder(@PathVariable Long id){
        if(id<0){
            throw new RuntimeException("id error is not < 0");
        }
        String replace = UUID.randomUUID().toString().replace("-", "");
        return Thread.currentThread().getName()+"  ：流水号  "+replace;
    }

    public String providerHystrixTimeOutHandler(){
        return "当前服务器忙，请稍后再试 /(T o T)/~";
    }
}
